package mx.frantor.util;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * 
 * @author Jorge Castellanos
 *
 */

public class HibernateUtil {
	
	private static final SessionFactory sessionFactory;
	private static final ServiceRegistry serviceRegistry;
	
	static{
		try{

			//Crear sessionFactory
			Configuration configuration = new Configuration().configure();
		    serviceRegistry = new ServiceRegistryBuilder()
				.applySettings(configuration.getProperties())
					.buildServiceRegistry();
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
			
		}catch(Throwable ex){
			//Si se genera error, mostrar en pantalla el mensaje
			System.err.println("Fallo en la creacion de la SessionFactory: " + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
	
	/**
	 * Gets the session factory.
	 *
	 * @return the session factory
	 * @throws HibernateException the Hibernate exception
	 */
	public static SessionFactory getSessionFactory() throws HibernateException {
		//Retornamos la sessionFactory generada (Session)
		return sessionFactory;
	}
	
}
